<?php

namespace App\Jobs;
use App\Models\User;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;


use Illuminate\Contracts\Mail\Mailer;

class ProcessPodcast implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $user;

    /**
     * 创建任务实例
     *
     * @param  Podcast  $podcast
     * @return void
     */


    public function __construct(User $user)
    {
        //
        $this->user = $user;


    }


    /**
     * 执行任务
     *
     * @param
     * @return void
     */
    public function handle()
    {
        //
        echo "队列执行";
        //执行任务操作
        $rand = mt_rand(1,2);
        if ($rand==2){
            sleep(3);
            throw new \Exception('任务失败');
        }
        $content = $this->user->username . "发表了新文章" . time() . "\n";
        file_put_contents('./notice.txt', $content, FILE_APPEND);
    }
    //失败处理
    public function failed(\Exception $exc){
        var_dump($exc->getMessage());
    }
}
